package com.sgb.member.ui.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Rect;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.lxj.xpopup.XPopup;
import com.sgb.member.R;
import com.sgb.member.databinding.ActivityChooseMemberTypeBinding;
import com.sgb.member.di.DaggerMemberComponent;
import com.sgb.member.di.MemberModule;
import com.sgb.member.ui.adapter.ChooseMemberTypeAdapter;
import com.sgb.member.view_model.ChooseMemberTypeModel;
import com.sgb.member.weight.MultiMemberChoosePopup;
import com.swgk.core.base.AppActivity;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.FlowLayoutManager;
import com.swgk.core.util.DisplayUtil;
import com.swgk.core.util.MToast;
import com.swgk.core.util.StringUtil;
import com.swgk.core.view.selection.CommonPopEntity;

import org.jetbrains.annotations.NotNull;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

import javax.inject.Inject;

/**
 * 会员类型选择
 */
public class ChooseMemberTypeActivity extends AppActivity {

    @Inject
    ChooseMemberTypeModel sModel;
    private ActivityChooseMemberTypeBinding mBinding;
    private ChooseMemberTypeAdapter mAdapter;

    private String ascription;
    private List<CommonPopEntity> mData;

    boolean singleton;

    // TODO: 2021/8/26 //类型选择
    // ascription 0 供方 1需方
    public static void start(Activity context, String ascription, int code) {
        Intent intent = new Intent(context, ChooseMemberTypeActivity.class);
        intent.putExtra("MEMBER_TYPE", ascription);
        context.startActivityForResult(intent, code);
    }

    // ascription 0 供方 1需方
    public static void start(Activity context, List<CommonPopEntity> data, String ascription, int code) {
        Intent intent = new Intent(context, ChooseMemberTypeActivity.class);
        intent.putExtra("MEMBER_DATA", (Serializable) data);
        intent.putExtra("MEMBER_TYPE", ascription);
        context.startActivityForResult(intent, code);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerMemberComponent.builder()
                .appComponent(appComponent)
                .memberModule(new MemberModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void initView() {
        ascription = getIntent().getStringExtra("MEMBER_TYPE");
        List<CommonPopEntity> data = (List<CommonPopEntity>) getIntent().getSerializableExtra("MEMBER_DATA");
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_choose_member_type);

        if (TextUtils.equals(ascription, "34")) {
            mBinding.include.titleView.setText("经营类型");
            mBinding.typeName.setText("经营类型");
        } else if (TextUtils.equals(ascription, "35")) {
            mBinding.include.titleView.setText("服务范围");
            mBinding.typeName.setText("服务范围");
        } else {
            mBinding.include.titleView.setText("会员类型");
            mBinding.typeName.setText("会员类型");
        }
        mBinding.recycler.setLayoutManager(new FlowLayoutManager());
        int size = DisplayUtil.dip2px(ChooseMemberTypeActivity.this, 10);
        mBinding.recycler.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull @NotNull Rect outRect, @NonNull @NotNull View view, @NonNull @NotNull RecyclerView parent, @NonNull @NotNull RecyclerView.State state) {
                outRect.set(0, 0, size, size);
            }
        });
        mAdapter = new ChooseMemberTypeAdapter(this, data);
        mBinding.recycler.setAdapter(mAdapter);

        mBinding.include.ivBack.setOnClickListener(this);
        mBinding.tvDeleteAll.setOnClickListener(this); //全部删除
        mBinding.tvFinish.setOnClickListener(this); // 完成
        mBinding.ivDelete.setOnClickListener(this); // 删除图标
        mBinding.btnAdd.setOnClickListener(this); // 添加图标
        mBinding.btnRelease.setOnClickListener(this); // 确定
        if (TextUtils.equals(ascription, "34")) {//经营类型
            sModel.getManagementTypesList();
        } else if (TextUtils.equals(ascription, "35")) {//服务范围
            sModel.getServiceTypeList();
        } else {
            sModel.getMemberTypeList(StringUtil.isEmpty(ascription) ? "" : ascription);
        }

    }

    @SuppressLint({"NonConstantResourceId", "NotifyDataSetChanged"})
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_back) {
            onBackPressed();
        } else if (id == R.id.tv_delete_all) {//全部删除
            mAdapter.getDatas().clear();
            mAdapter.notifyDataSetChanged();
        } else if (id == R.id.tv_finish) {// 完成
            mAdapter.setDelete(false);
            mBinding.deleteLayout.setVisibility(View.GONE);
            mBinding.ivDelete.setVisibility(View.VISIBLE);
        } else if (id == R.id.iv_delete) {// 删除图标
            mAdapter.setDelete(true);
            mBinding.deleteLayout.setVisibility(View.VISIBLE);
            mBinding.ivDelete.setVisibility(View.GONE);
        } else if (id == R.id.btn_add) {// 添加图标
            if (mData == null || mData.size() == 0) {
                if (TextUtils.equals(ascription, "34")) {//经营类型
                    sModel.getManagementTypesList();
                } else if (TextUtils.equals(ascription, "35")) {//服务范围
                    sModel.getServiceTypeList();
                } else {
                    sModel.getMemberTypeList(StringUtil.isEmpty(ascription) ? "" : ascription);
                }
                return;
            }
            if (TextUtils.equals(ascription, "34")) {//经营类型
                if (mAdapter.getDatas().size() > 10) {
                    MToast.showToast(this, "经营类型最多显示10条");
                    return;
                }
            } else if (TextUtils.equals(ascription, "35")) {//服务范围
                if (mAdapter.getDatas().size() > 10) {
                    MToast.showToast(this, "服务范围最多显示10条");
                    return;
                }

            } else {
                if (mAdapter.getDatas().size() > 20) {
                    MToast.showToast(this, "会员类型最多显示20条");
                    return;
                }
            }
            int height = mBinding.include.topLayout.getHeight();
            new XPopup.Builder(getActivity())
                    .asCustom(new MultiMemberChoosePopup(getActivity(), height, mData, mBinding.include.titleView.getText().toString(), new MultiMemberChoosePopup.OnConfirmListener() {
                        @Override
                        public void onConfirm(CommonPopEntity entity) {
                            List<CommonPopEntity> datas = mAdapter.getDatas();
                            if (datas != null && datas.size() > 0) {
                                for (CommonPopEntity data : datas) {
                                    if (TextUtils.equals(data.getTypeName(), entity.getTypeName()) && TextUtils.equals(data.getNo(), entity.getNo())) {
                                        MToast.showToast(getActivity(), "请选择不同的类型");
                                        return;
                                    }
                                }
                            }
                            mAdapter.addData(entity);
                        }
                    })).show();
        } else if (id == R.id.btn_release) {// 确定
            int size = mAdapter.getDatas().size();
            if (size == 0) {
                if (TextUtils.equals(ascription, "34")) {//经营类型
                    MToast.showToast(this, "至少选择一个经营类型");
                    return;
                } else if (TextUtils.equals(ascription, "35")) {//服务范围
                    MToast.showToast(this, "至少选择一个服务范围");
                    return;
                } else {
                    MToast.showToast(this, "至少选择一个会员类型");
                    return;
                }
            }
            Intent intent = new Intent();
            intent.putExtra("MEMBER_DATA", (Serializable) mAdapter.getDatas());
            setResult(RESULT_OK, intent);
            finish();
        }
    }

    public void setMemberTypes(List<CommonPopEntity> data) {
        mData = data;
    }
}
